<template>
  <div>App</div>
  <button @click="sendMessage">发送消息</button>
</template>

<script lang="ts" setup>
defineOptions({
  name: "App",
});

// 客户端连接ws服务器
const ws = new WebSocket("ws://localhost:3000");

ws.onopen = () => {
  console.log("客户端连接ws服务器成功了");
};

ws.onerror = (e) => {
  console.log("客户端连接ws服务器失败了", e);
};

ws.onclose = (e) => {
  console.log("ws服务器断开链接了", e);
};
// 给ws服务器发送消息
const sendMessage = () => {
  ws.send("hello ws");
};

// 接受服务器的消息
ws.onmessage = (e) => {
  console.log("客户端接受服务器的消息: ", e.data);
};
</script>

<style scoped lang="scss"></style>
